# coding=utf-8
import csv
import numpy as np
import tensorflow.compat.v1 as tf
import math
tf.disable_v2_behavior()

def add_layer(inputs, in_size, out_size, activation_function=None):
    # add one more layer and return the output of this layer
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]))
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

cvs_data = []
with open("data.csv") as csvfile:
    csv_reader = csv.reader(csvfile)  # 使用csv.reader读取csvfile中的文件
    for row in csv_reader:  # 将csv 文件中的数据保存到birth_data中
        cvs_data.append(row)

cvs_data = [[float(x) for x in row] for row in cvs_data]  # 将数据从string形式转换为float形式
cvs_data = np.array(cvs_data)
x_data = cvs_data[:, 0:4]
y_data = cvs_data[:, 5:6]


print('x1=\r\n%s' % (np.linspace(-math.pi,math.pi,10)[:, np.newaxis]))
print('x=\r\n%s' % (x_data))
print('y=\r\n%s' % (y_data))
# define placeholder for inp
# uts to network
xs = tf.placeholder(tf.float32, [None, 4])
ys = tf.placeholder(tf.float32, [None, 1])

# add hidden layer
l1 = add_layer(xs, 4, 8, activation_function=tf.nn.sigmoid)
# add output layer
prediction = add_layer(l1, 8, 1)

loss = tf.reduce_mean(tf.square(ys - prediction))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    # training
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})

prediction_value = sess.run(prediction, feed_dict={xs: x_data})

print("prediction_value=",prediction_value)